package com.wits.platform.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import com.wits.platform.common.constants.SysTabConstants;
@Entity
@Table(name = "wits_menu")
public class Menu extends BaseEntity{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2066859386769351419L;

	public Menu() {

	}

	public Menu(String muId, String muName) {
		this.muId = muId;
		this.muName = muName;
	}
	
	/**
	 * 菜单编号
	 */
	private String muId;
	
	/**
	 * 语言码
	 */
	private String langType;

	/**
	 * 上级菜单
	 */
	private Menu parent;

	/**
	 * 链接地址
	 */
	private String muHref;
	
	/**
	 * 菜单名称
	 */
	private String muName;
	
	/**
	 * 功能介绍
	 */
	private String muDesc;

	/**
	 * 排序
	 */
	private Integer muOrd;
	
	/**
	 * 是否可代理
	 */
	private String isDeputy=SysTabConstants.SYSTAB_D_TNF_F;

	/**
	 * 窗口锚标记
	 */
	private String muRel;
	
	private String muIcon;


	private List<Menu> children = new ArrayList<Menu>();

	private List<Role> roles;
	
	private List<Resource> resources;



	private String checked;
	
	@Id
	@Column(name="mu_Id",length=36)
	public String getMuId() {
		return muId;
	}

	public void setMuId(String muId) {
		this.muId = muId;
	}

	@Column(name="lang_type",length=20)
	public String getLangType() {
		return langType;
	}

	public void setLangType(String langType) {
		this.langType = langType;
	}

	@Column(name="mu_href",length=100)
	public String getMuHref() {
		return muHref;
	}

	public void setMuHref(String muHref) {
		this.muHref = muHref;
	}

	@Column(name="mu_name",length=20)
	public String getMuName() {
		return muName;
	}

	public void setMuName(String muName) {
		this.muName = muName;
	}

	@Column(name="mu_desc",length=100)
	public String getMuDesc() {
		return muDesc;
	}

	public void setMuDesc(String muDesc) {
		this.muDesc = muDesc;
	}

	@Column(name="mu_ord")
	public Integer getMuOrd() {
		return muOrd;
	}

	public void setMuOrd(Integer muOrd) {
		this.muOrd = muOrd;
	}

	@Column(name="is_deputy",length=1)
	public String getIsDeputy() {
		return isDeputy;
	}

	public void setIsDeputy(String isDeputy) {
		this.isDeputy = isDeputy;
	}


	@Column(name="mu_rel",length=20)
	public String getMuRel() {
		return muRel;
	}

	public void setMuRel(String muRel) {
		this.muRel = muRel;
	}
	
	@Column(name="mu_icon",length=100)
	public String getMuIcon() {
		return muIcon;
	}

	public void setMuIcon(String muIcon) {
		this.muIcon = muIcon;
	}
	
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "menus")
	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	@ManyToOne()
	@JoinColumn(name = "mu_id_p", nullable = true)
	public Menu getParent() {
		return this.parent;
	}

	public void setParent(Menu parent) {
		this.parent = parent;
	}



	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY,cascade={CascadeType.REMOVE,CascadeType.REFRESH})
	public List<Menu> getChildren() {
		return this.children;
	}

	public void setChildren(List<Menu> children) {
		this.children = children;
	}
	
	@OneToMany(mappedBy = "menu", fetch = FetchType.LAZY,cascade={CascadeType.REMOVE,CascadeType.REFRESH})
	public List<Resource> getResources() {
		return resources;
	}

	public void setResources(List<Resource> resources) {
		this.resources = resources;
	}

	@Transient
	public String getChecked() {
		return checked;
	}

	public void setChecked(String checked) {
		this.checked = checked;
	}
	private static JsonConfig jsonConfig = new JsonConfig();

	@Transient
	public static JsonConfig getJsonConfig() {
		return jsonConfig;
	}

	static {
		jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object arg0, String name, Object arg2) {
				if (name.equals("roles") || name.equals("children")
						|| name.equals("jsonConfig") || name.equals("dateCre")
						|| name.equals("dateUpd") || name.equals("resources")) {
					return true;
				}
				return false;
			}
		});
	}
	
	@Override
	public String toString() {
		String pid ="";
		String pName ="";
		boolean isParent=true;
		if(this.parent!=null){
			isParent=false;
			pid=this.parent.muId;
			pName=this.parent.muName;
		}
		return "{\"id\":\"" + this.muId 
				+ "\",\"pId\":\"" + pid
				+ "\",\"name\":\"" + this.muName 
				+ "\",\"pName\":\""+pName
				+"\",\"isParent\":"+isParent
				+",\"muIcon\":\""+this.muIcon
				+"\",\"muHref\":\""+this.muHref
				+"\",\"muRel\":\""+this.muRel
				+"\",\"checked\":"+this.checked
				+"}";
	}
	
	
	/**
	 * 授权调用
	 * @return
	 */
	public String toRightTree(){
		String pid ="1";
		boolean isParent=true;
		if(this.parent!=null){
			isParent=false;
			pid=this.parent.muId;
		}
		StringBuilder str=new StringBuilder();
		str.append("{\"id\":\"").append(this.muId)
			.append("\",\"name\":\"").append(this.muName)
		    .append("\",\"pId\":\"").append(pid)
		    .append("\",\"type\":\"menu")
		    .append("\",\"isParent\":").append(isParent)
		    .append("}");
		
		return str.toString();
	}
	

	@Override
	public String display() {
		return this.muName;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((muId == null) ? 0 : muId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Menu other = (Menu) obj;
		if (muId == null) {
			if (other.muId != null)
				return false;
		} else if (!muId.equals(other.muId))
			return false;
		return true;
	}
	
	
}